//
//  SDChooseGameController.m
//  Sudoku
//
//  Created by wxlan on 16/6/16.
//  Copyright © 2016年 wxlan. All rights reserved.
//

#import "SDChooseGameController.h"
#import "SDChooseGameView.h"
#import "SDGameFlowLayout.h"
#import "SDGameManager.h"
#import "SDLevelDetailModel.h"

@interface SDChooseGameController ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, weak) UICollectionView *easyCollectionView;
@property (nonatomic, weak) UICollectionView *normalCollectionView;
@property (nonatomic, weak) UICollectionView *hardCollectionView;


@end

@implementation SDChooseGameController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self buildUI];
    
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    NSArray *lastGamePath = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastGame"];
    NSInteger lastGameSection = [lastGamePath.firstObject integerValue];
    NSInteger lastGameRow = [lastGamePath.lastObject integerValue];
    
    UICollectionView *tempCollectionView ;
    if (lastGameSection == 0) {
        tempCollectionView = self.easyCollectionView;
    }else if (lastGameSection == 1)
    {
        tempCollectionView = self.normalCollectionView;
    }else
    {
        tempCollectionView = self.hardCollectionView;
    }
    [tempCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:lastGameRow inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
//    [tempCollectionView setContentOffset:CGPointMake(800, 0)];

}
- (void)buildUI
{
    UIView *topView = [[UIView alloc] init];
    topView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.equalTo(@(64));
    }];
    UIButton *leftButton = [[UIButton alloc] init];
    [topView addSubview:leftButton];
    leftButton.titleLabel.textAlignment = NSTextAlignmentLeft;
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(50));
        make.left.equalTo(topView).with.offset(10);
        make.top.bottom.equalTo(topView).offset(10);
    }];
    @weakify(self)
    leftButton.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        [self dismissViewControllerAnimated:YES completion:nil];
        return [RACSignal empty];
    }];
//    UIView  *divdingView = [[UIView alloc] init];
//    divdingView.backgroundColor = [UIColor blackColor];
//    [topView addSubview:divdingView];
//    [divdingView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.bottom.equalTo(topView);
//        make.height.equalTo(@(1));
//    }];
    
    UIView *chooseGameView = [[UIView alloc] init];
    [self.view addSubview:chooseGameView];
    [chooseGameView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(@(64));
    }];
    
    CGFloat margin = 10;
    NSInteger cellCount  = 3;
    CGFloat cellHeight = (self.view.height-64-margin+1*cellCount)/cellCount;
    NSArray *leverArray = @[@"初\n级\n题",@"中\n级\n题",@"高\n级\n题"];
    

    for (int i = 0; i < cellCount; i++) {
        UIView *levelView = [[UIView alloc] init];
        [chooseGameView addSubview:levelView];
        [levelView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(cellHeight));
            make.left.equalTo(chooseGameView);
            make.right.equalTo(chooseGameView);
            make.top.equalTo(@(i* (margin + cellHeight)+ margin));
        }];
        
        UILabel *levelLabel = [[UILabel alloc] init];
        [levelView addSubview:levelLabel];
        levelLabel.numberOfLines = 0;
        levelLabel.textAlignment = NSTextAlignmentCenter;
        levelLabel.text = leverArray[i];
        [levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(levelView);
            make.width.equalTo(@(50));
        }];
        
        SDGameFlowLayout *layout = [[SDGameFlowLayout alloc] init];
        layout.cellCount = [[SDGameManager sharedSDGameManager] numberOfAskAtSection:i];
//        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        collectionView.backgroundColor = [UIColor whiteColor];
        
        collectionView.delegate = self;
        collectionView.dataSource = self;
        [chooseGameView addSubview:collectionView];
        [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(levelLabel.mas_right);
            make.right.top.bottom.equalTo(levelView);
        }];
        switch (i) {
            case 0:
            {
                self.easyCollectionView = collectionView;
                break;
            }
             case 1:
            {
                self.normalCollectionView = collectionView;
                break;
            }
            case 2:
            {
                self.hardCollectionView = collectionView;
                break;
            }

            default:
                break;
        }
    }



}
//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
//{
//    return 9;
//}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == self.easyCollectionView) {
        return [[SDGameManager sharedSDGameManager] numberOfAskAtSection:0];
    }else if (collectionView == self.normalCollectionView)
    {
        return [[SDGameManager sharedSDGameManager] numberOfAskAtSection:1];

    }else
    {
        return [[SDGameManager sharedSDGameManager] numberOfAskAtSection:2];

    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identify = @"cell";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identify forIndexPath:indexPath];
    UILabel *label = (UILabel *)[cell.contentView viewWithTag:11];
    if (label == nil) {
        label = [[UILabel alloc] init];
        
        label.layer.borderWidth = 0.5;
        label.layer.borderColor = [UIColor grayColor].CGColor;
        label.textAlignment = NSTextAlignmentCenter;
//        label.textColor = [UIColor blackColor];
        label.tag = 11;
        [cell.contentView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(cell.contentView);
        }];
        label.text = [NSString stringWithFormat:@"%ld", indexPath.row+1];


    }
    SDLevelDetailModel *model;
    

    if (collectionView == self.easyCollectionView) {
        model = [[SDGameManager sharedSDGameManager] getOneGameDataWithIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0]];
        
    }else if (collectionView == self.normalCollectionView)
    {
        model = [[SDGameManager sharedSDGameManager] getOneGameDataWithIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:1]];

        
    }else
    {
        model = [[SDGameManager sharedSDGameManager] getOneGameDataWithIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:2]];

        
    }
//    NSLog(@"===%ld", model.playState);
    if (model.playState == LevelPlayStateSolved) {
        label.textColor = [UIColor greenColor];
    }else if (model.playState == LevelPlayStatePlaying)
    {
        label.textColor = [UIColor blueColor];
    }else
    {
        label.textColor = [UIColor blackColor];
    }
    label.text = [NSString stringWithFormat:@"%ld", indexPath.row+1];
    
    NSArray *lastGamePath = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastGame"];
    NSInteger lastGameSection = [lastGamePath.firstObject integerValue];
    NSInteger lastGameRow = [lastGamePath.lastObject integerValue];
    UICollectionView *tempCollectionView;
    if (lastGameSection == 0) {
        tempCollectionView = self.easyCollectionView;
    }else if (lastGameSection == 1)
    {
        tempCollectionView = self.normalCollectionView;
    }else
    {
        tempCollectionView = self.hardCollectionView;
    }
    if (collectionView == tempCollectionView && indexPath.row == lastGameRow) {
        label.backgroundColor = mRGBColor(135, 206, 235);
    }else
    {
        label.backgroundColor = [UIColor whiteColor];
        
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *path;
    if (collectionView == self.easyCollectionView) {
        path = @[@(0),@(indexPath.row)];
    }else if (collectionView == self.normalCollectionView)
    {
        path = @[@(1),@(indexPath.row)];
        
    }else
    {
        path = @[@(2),@(indexPath.row)];
        
    }
//
//    [[NSUserDefaults standardUserDefaults] setObject:path forKey:@"lastGame"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"chooseOtherGameNotification" object:nil userInfo:@{@"userInfo":path }];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
////    CGFloat itemWidth = ((self.view.width - 50-10)/2)/3;
////    return CGSizeMake(itemWidth, itemWidth);
//    return CGSizeMake(30, 30);
//    
//}
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
//    return UIEdgeInsetsMake(0, 0, 0, 0);
//}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
